package com.songshuai.pangu.api.controller;

import com.songshuai.webapi.baseapi.model.custminiprogram.YixueBaseCity;
import com.songshuai.pangu.api.model.base.YixueBaseDictionary;
import com.songshuai.pangu.api.service.CommonService;
import com.songshuai.pangu.api.service.YixueBaseDictionaryService;
import com.songshuai.webapi.baseapi.dto.feign.response.DictionaryDto;
import com.songshuai.webapi.baseapi.enumerations.ResultStatus;
import com.songshuai.webapi.baseapi.exception.BusinessException;
import com.songshuai.webapi.baseapi.pojo.QueryRes;
import com.songshuai.webapi.baseapi.pojo.QueryResponse;
import com.songshuai.webapi.baseapi.utils.OssUploadUtil;
import com.songshuai.webapi.baseapi.utils.PropertiesUtil;
import com.songshuai.webapi.baseapi.utils.SnowFlakeUtil;
import com.songshuai.webapi.common.upload.BaseUploadFile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: lhx
 * @Date: 2019/3/22 14:55
 */
@Slf4j
@RestController
@RequestMapping("/api/common/")
public class CommonController {

    @Autowired
    private YixueBaseDictionaryService baseDictionaryService;

    @Autowired
    private CommonService commonService;

    @Autowired
    private SnowFlakeUtil snowFlakeUtil;

    @Autowired
    private OssUploadUtil uploadUtil;

    @Value("${file.basePath}")
    private String fileBasePath;





    /**
     * 根据字典类型查询字典
     * @param type
     * @return
     */
    @Cacheable(value = "dictionary")
    @GetMapping("queryDictionariesByType")
    public QueryResponse<List<DictionaryDto>> queryDictionariesByType(@RequestParam("type")Integer type){
        try {
            List<YixueBaseDictionary> yixueBaseDictionaries = baseDictionaryService.queryByType(type);
            return QueryResponse.setResData(yixueBaseDictionaries.stream().map(dict -> {
                DictionaryDto dictionary = new DictionaryDto();
                PropertiesUtil.copyProperties(dict, dictionary);
                return dictionary;
            }).collect(Collectors.toList()));
        } catch (Exception e) {
            log.error("/api/common/queryDictionariesByType",e);
        }
        return new QueryResponse<>(ResultStatus.ERROR_SEARCH_FAILED);
    }

//    @Cacheable("Area")
    @GetMapping("queryAreaByParentId")
    public QueryResponse<List<YixueBaseCity>> queryAreaByParentId(@RequestParam("pId") Integer pId){
        List<YixueBaseCity> yixueBaseCities = baseDictionaryService.queryAreaByParentId(pId);
        return QueryResponse.setResData(yixueBaseCities);
    }


    /**
     * 根据id查询字典详情
     * @param id
     * @return
     */
    @GetMapping("queryDictionaryDetail")
    public QueryResponse<DictionaryDto> queryDictionaryDetail(@RequestParam("id")Integer id){
        YixueBaseDictionary yixueBaseDictionary = baseDictionaryService.queryDetail(id);
        DictionaryDto result = new DictionaryDto();
        PropertiesUtil.copyProperties(yixueBaseDictionary,result);
        return QueryResponse.setResData(result);
    }



    /**上传图片   七牛云小程序客户端(回执单)*/
    @PostMapping("uploadPicture")
    public QueryResponse uploadPicture(@RequestPart("file") MultipartFile file, @RequestParam("imgMaxSize") Integer imgMaxSize,
                                  @RequestParam("basePath")String basePath, @RequestParam("imgPath")String imgPath) {
        try{
            return commonService.uploadPicture(file, imgMaxSize, basePath, imgPath);
        } catch (Exception ex) {
            log.error("api/common/uploadPicture", ex);
            throw new BusinessException(ResultStatus.ERROR_PIC_UPLOAD.getErrorMsg(), ResultStatus.FAILOfficial);
        }
    }

    /**上传图片  阿里云*/
    @PostMapping("uploadPictureOss")
    public QueryResponse uploadPictureOss(@RequestPart("file") MultipartFile file) {
        try{
            BaseUploadFile baseUploadFile=new BaseUploadFile(snowFlakeUtil,fileBasePath,uploadUtil);
            Map<String, Object> map = baseUploadFile.uploadFile(file,"image");
            Integer code=Integer.parseInt(String.valueOf(map.get("code")));
            String msg=String.valueOf(map.get("msg"));
            map.remove("code");
            map.remove("msg");
            return QueryResponse.setResData(map,code,msg);
        } catch (Exception ex) {
            log.error("api/common/uploadPictureOss", ex);
            return new QueryResponse<>(ResultStatus.FAILOfficial.getErrorCode(),ex.getMessage());
        }
    }


    /**上传图片   七牛云   后台上传封面*/
    @PostMapping("uploadPictureSop")
    public QueryRes uploadPictureSop(@RequestPart("file") MultipartFile file, @RequestParam("imgMaxSize") Integer imgMaxSize,
                                       @RequestParam("basePath")String basePath, @RequestParam("imgPath")String imgPath) {
        try{
            return commonService.uploadPictureSop(file, imgMaxSize, basePath, imgPath);
        } catch (Exception ex) {
            log.error("api/common/uploadPictureSop", ex);
            throw new BusinessException(ResultStatus.ERROR_PIC_UPLOAD.getErrorMsg(), ResultStatus.FAILOfficial);
        }
    }
}
